import routerIndex from './router/index.js';
import fs from "fs";
import path from "path";

let buildPlugins = []
if (process.env.NODE_ENV === 'production') {
  buildPlugins.push("transform-remove-console")
}

export default {
  hooks: {
    'render:route': (url, result) => {
      const nuxtContent = result.html.match(/<script>(window.__NUXT__.+?)<\/script>/)[1];
      if (process.env.NODE_ENV !== "development") {
        const nuxtFilePath = path.join(process.cwd(), `.nuxt/dist/client/init.js`);
        fs.writeFileSync(nuxtFilePath, nuxtContent);
        result.html = result.html.replace(/<script>window.__NUXT__.+?<\/script>/, `<script src="/_nuxt/init.js?t=${new Date().getTime()}"></script>`);
      }
      result.html = result.html.replace(/data-n-head=\"ssr\"/gi,'')
    }
  },
  rootDir: process.cwd(),
  srcDir: process.cwd(),
  buildDir: 'nuxt-dist',
  head: {
    title: 'enterprise-china.com',
    bodyAttrs: { class: 'body-class' },
    htmlAttrs: {
      lang: 'en'
    },
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=0.1, 杭州远恒科技有限公司-scale=1.0,user-scalable=no' },
      { name: 'robots', content: 'INDEX,FOLLOW' },
      { 'http-equiv': 'X-UA-Compatible', content: 'Chrome=1,IE=Edge' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
      { rel: 'stylesheet', href: '/styles/icon/iconfont.css' }
    ],
    script: [
      // {src: "https://unpkg.com/@wangeditor/editor@latest/dist/index.js"},
      // {src: "https://www.googletagmanager.com/gtag/js?id=具体的统计项目id", async: true},
      { 
        hid: 'ldjson-schema',
        innerHTML: "window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', '具体的统计项目id'); ",
      },
      {src: "/common.js", async: true},
      {src: "/gtag.js?id=具体的统计项目id", async: true},
    ],
    __dangerouslyDisableSanitizersByTagID: {
      'ldjson-schema': ['innerHTML']
    }
  },
  build: {
    transpile: [/^element-ui/],
    vendor: ['vue-i18n', 'babel-polyfill', 'echarts'],
    babel: {
      plugins: buildPlugins
    },
    extractCSS: { allChunks: true }
  },
  css: [
    'element-ui/lib/theme-chalk/index.css',
    'swiper/css/swiper.css',
    '@/static/styles/index.scss',
    '~static/styles/editor.css', // 编辑器css
    'quill/dist/quill.snow.css',
    'quill/dist/quill.bubble.css',
    'quill/dist/quill.core.css'
  ],
  plugins: [
    '@/plugins/element-ui',
    { src:'@/plugins/baidu', mode:'client', ssr: false },
    { src: '~plugins/ga.js', mode:'client', ssr: false },
    '@/plugins/i18n.js',
    { src: '@/plugins/nuxt-swiper-plugin.js', ssr: false },
    '@/plugins/api.js',
    '@/plugins/tools.js',
    { src:'@/plugins/echarts.js', mode:'client', ssr: false},
    { src:'@/plugins/mescroll.js'},
    { src:'@/plugins/route.js'},
    { src:'@/plugins/bus.js'},
    { src:'@/plugins/directive.js', mode:'client', ssr: false},
    { src: '@/plugins/vue-quill-editor', ssr: false },
    { src: '@/plugins/jquery.js', ssr: false },
    { src: '@/plugins/jsencrypt.js', ssr: false },
    { src: '@/plugins/file-saver.js', ssr: false },
  ],
  components: true,
  buildModules: [
    '@nuxtjs/style-resources'
  ],
  styleResources: {
    scss: [], // 可以是数组也可以是字符串
  },
  modules: [
    //有参数配置的
    'cookie-universal-nuxt', ['cookie-universal-nuxt', {
      parseJSON: true
    }],
    '@nuxtjs/auth-next',
    '@nuxtjs/dotenv'
  ],
  auth: {
    // Options
  },
  router: {
    middleware: 'i18n',
    extendRoutes(routes, resolve) {
      // 清空路由数组
      routes.splice(0);
      // 插入新的路由配置
      routes.push(...routerIndex.getRoutes(resolve));
    },
    scrollBehavior (to, from, savedPosition) {
      return { x: 0, y: 0 }
    }
  },
  generate: {
    workers: 4,
    workerConcurrency: 500,
    concurrency: 500,
    // routes (callback, params) {
    //   return axios.get('https://api.example.com/routes?since=' + params.lastFinished)
    //   .then((res) => {
    //     return res.data
    //   })
    // },
    // done ({ duration, errors, workerInfo }) {
    //   if (errors.length) {
    //     axios.post('https://api.example.com/routes', { generate_errors: errors })
    //   }
    // }
  }
}
